package com.erp.servlet;

import com.erp.bean.Commodity;
import com.erp.bean.Inventory;
import com.erp.service.CommodityService;
import com.erp.service.InventoryService;
import com.erp.service.impl.CommodityServiceImpl;
import com.erp.service.impl.InventoryServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author lyb
 * 创建商品调度控制器
 */
@WebServlet("/dispacommond.action")
public class DispaCommondServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取用户选择的商品以及数量
        String cogid=req.getParameter("cogid");
        String conum=req.getParameter("conum");
        //查询库存中的cogid商品数量
        InventoryService inventoryService=new InventoryServiceImpl();
        CommodityService commodityService=new CommodityServiceImpl();
        //类型转化
        int gid=Integer.valueOf(cogid);
        int num=Integer.valueOf(conum);
        String outid=req.getParameter("outid");
        String inputid=req.getParameter("inputid");
        System.out.println(inputid);
        int pid=Integer.valueOf(inputid);
        System.out.println(pid);
        int tid=Integer.valueOf(outid);
        Inventory invents=inventoryService.findByGidob(gid,tid);
       int gnum=invents.getNum();
        if (gnum<num){
            //存入错误信息
            req.setAttribute("ms","库存数量不足请修改数量或去采购");
            //接收出库id
            req.setAttribute("inputid",pid);
            req.setAttribute("outid",tid);
            List<Inventory> invens=inventoryService.findByEntid(tid);

            List<Commodity> commcods= commodityService.findAll();
            req.setAttribute("invens",invens);
            req.setAttribute("commcods",commcods);

            //转发到原页面
            req.getRequestDispatcher("dispacommond.jsp").forward(req,resp);
        }else{
            //修改出库库存
            Inventory inventory=inventoryService.findByGidob(gid,tid);
            System.out.println(inventory);
            inventory.setNum(inventory.getNum()-num);
            inventoryService.update(inventory);
            //修改入库库存

            Inventory inventory2=inventoryService.findByGidob(gid,pid);
            if (inventory2==null){
                Inventory inventory3=new Inventory(gid,num,pid);
                inventoryService.add(inventory3);
            }else {
                System.out.println(inventory2);
                inventory2.setNum(inventory2.getNum()+num);
                inventoryService.update(inventory2);
            }

            //重定向到库存列表控制器
            resp.sendRedirect("inventoryall.action");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
